package com.macro.mall.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsLabelParam;
import com.macro.mall.mapper.BmsLabelMapper;
import com.macro.mall.model.BmsLabel;
import com.macro.mall.service.BmsLabelService;
import com.macro.mall.utils.UserUtils;

import cn.hutool.core.date.DateTime;

@Service
public class BmsLabelServiceImpl implements BmsLabelService {
    
    @Autowired
    private UserUtils userUtils;
    
    @Autowired
    private BmsLabelMapper bmsLabelMapper;

    @Override
    @Transactional
    public int create(BmsLabelParam bmsLabelParam) {
        BmsLabel bmsLabel = new BmsLabel(); 
        BeanUtils.copyProperties(bmsLabelParam, bmsLabel);
        Long userId = userUtils.getUserId();
        bmsLabel.setCreateUser(userId);
        bmsLabel.setUpdateUser(userId);
        bmsLabel.setCreateTime(new DateTime());
        bmsLabel.setUpdateTime(new DateTime());
        int count = bmsLabelMapper.insertBmsLabel(bmsLabel);
        return count;
    }

    @Override
    @Transactional
    public int update(BmsLabelParam bmsLabelParam) {
        BmsLabel bmsLabel = new BmsLabel(); 
        BeanUtils.copyProperties(bmsLabelParam, bmsLabel);
        Long userId = userUtils.getUserId();
        bmsLabel.setUpdateUser(userId);
        bmsLabel.setUpdateTime(new DateTime());
        int count = bmsLabelMapper.updateBmsLabel(bmsLabel);
        return count;
    }

    @Override
    public List<BmsLabel> getList(BmsLabelParam bmsLabelParam) {
        PageHelper.startPage(bmsLabelParam.getPageNum(), bmsLabelParam.getPageSize());
        return bmsLabelMapper.list();
    }

    @Override
    public BmsLabel getItem(Long id) {
        return bmsLabelMapper.selectById(id);
    }

    @Override
    @Transactional
    public int delete(List<Long> ids) {
        BmsLabel bmsLabel = new BmsLabel(); 
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        bmsLabel.setIsDelete(true);
        bmsLabel.setUpdateUser(userId);
        bmsLabel.setUpdateTime(new DateTime());
        
        int count = bmsLabelMapper.updateDeleteId(ids);
        return count;
    }
}
